package evil.wolf.gui.login;

import evil.wolf.components.MyJButton;
import evil.wolf.gui.MainUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;




@SuppressWarnings("serial")
public class LoginFrame extends JDialog { 
	/**
	 * @author Haanz
	 */
	static int lenght = 30;
	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	MainUI mainUI;
	MyJButton btdangnhap = new MyJButton("Login");
	MyJButton btHuy = new MyJButton("Cancel");
	MyJButton btServer = new MyJButton("Server");
	boolean serverselected = false;
	
	
	// Contructor
	public LoginFrame(MainUI mainUI){
		this.mainUI = mainUI;
		JFrame.setDefaultLookAndFeelDecorated(true);
		try{
			setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//			setUndecorated(true);
//			getRootPane().setWindowDecorationStyle(JRootPane.NONE);
			initComponent();
			mainUI.setVisible(true);
			
			this.setResizable(false);
			this.setLocation((dim.width - getWidth())/2, (dim.height - getHeight())/2);
			this.setAlwaysOnTop(true);
			this.setVisible(true);
			btHuy.setToolTipText("Height " + this.serverPanel().getHeight() + " + Width" + serverPanel().getWidth());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public String title (){
		return this.getTitle();
	}
	
	@SuppressWarnings("rawtypes")
	public void initComponent(){
		
		this.setTitle("Login Form");
		setSize(340, 190);
		
		JPanel pg1 = new JPanel(new BorderLayout(10, 0));
		pg1.setBorder(BorderFactory.createEmptyBorder(0, -10, 0, 0));
		pg1.setBorder(new TitledBorder(new EtchedBorder(),"   Login   "));
		JPanel plabelUser = new JPanel(new GridLayout(2, 1, 10, 10));
		JPanel ptextUser = new JPanel(new GridLayout(2, 1, 5, 5));
		pg1.add(plabelUser, BorderLayout.WEST);
		pg1.add(ptextUser, BorderLayout.CENTER);
		
		JPanel pg2 = new JPanel();
		pg2.setLayout(new BoxLayout(pg2, BoxLayout.Y_AXIS));
		
		JPanel pg22 = new  JPanel();
		pg22.setAlignmentX(7f);
		pg22.setLayout(new BoxLayout(pg22, BoxLayout.X_AXIS));
	
		pg22.add(Box.createRigidArea(new Dimension(26, 0)));
		pg22.add(btServer);
		pg22.add(Box.createRigidArea(new Dimension(7, 0)));
		pg22.add(btdangnhap);
		pg22.add(Box.createRigidArea(new Dimension(7, 0)));
		pg22.add(btHuy);
		pg22.add(Box.createRigidArea(new Dimension(75, 0)));
		
		pg2.add(pg22);
		
		
		btHuy.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(10);
			}
		});
		
		final LoginFrame ds;
		ds = this;
		
		btdangnhap.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ds.setVisible(false);
				mainUI.setEnabled(true);
//				mainUI.isActive();
				
				mainUI.setVisible(true);
//				mainFrame.isFocused();
			}
		});
		
		btServer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				serverselected = !serverselected;
				if(serverselected)
					setSize(340, 305);
				else
					setSize(340, 190);
			}
		});

		JLabel lbusername = new JLabel("User Name");
//		lbusername.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		JLabel lbpass = new JLabel("Password");
//		lbpass.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		JComboBox cbxusername  = new JComboBox();
//		cbxusername.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 7)); //--    -- left --    -- right
//		cbxusername.setPreferredSize(new Dimension(00, 30));
//		cbxusername.setSize(5, lenght);
		cbxusername.setEditable(true);
		
		JPasswordField txtpass = new JPasswordField(lenght);
//		txtpass.setPreferredSize(new Dimension(30, 30));
//		txtpass.setBorder(BorderFactory.createCompoundBorder(new Border(new Dimension(cbxusername.getHeight(), cbxusername.getWidth())), new Border( new Dimension(cbxusername.getHeight(), cbxusername.getWidth()));
		plabelUser.add(lbusername);
//		plabelUser.add(Box.createRigidArea(new Dimension(1, 0)));
		plabelUser.add(lbpass);
		
		ptextUser.add(cbxusername);
		ptextUser.add(txtpass);
		
		JLabel picLabel = new JLabel(new ImageIcon("Images\\title.png"));
		picLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		//CollapsablePanel cp = new CollapsablePanel(this, "Cấu hình máy chủ", serverPanel());
		JPanel pwhole = new JPanel();
		
		pwhole.add(picLabel);
		pwhole.add(pg1);
		pwhole.add(pg2);
//		pwhole.add(s);
		pwhole.add(serverPanel());
//		pwhole.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		add(pwhole);
		
	}
	
	@SuppressWarnings("rawtypes")
	public JPanel serverPanel() {

		JPanel labelServer = new JPanel(new GridLayout(3, 1, 5, 5));
		JPanel textServer = new JPanel(new GridLayout(3, 1, 5, 5));
		JPanel pg3 = new JPanel(new BorderLayout(5, 5));
		pg3.setBorder(new TitledBorder(new EtchedBorder(),"   Server Configuration   "));
		pg3.add(labelServer, BorderLayout.WEST);
		pg3.add(textServer, BorderLayout.CENTER);
		
		JLabel lbmaychu = new JLabel("Host Name");
		lbmaychu.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
		JLabel lbcsdl = new JLabel("Database");
		JLabel lbmatkhau = new JLabel("Password");
		
		JComboBox cbxmaychu  = new JComboBox();
		cbxmaychu.setEditable(true);
		JComboBox cbxcsdl  = new JComboBox( );
		cbxcsdl.setEditable(true);
		JPasswordField cbxmatkhau  = new JPasswordField(lenght);
		
		labelServer.add(lbmaychu);
		labelServer.add(lbcsdl);
		labelServer.add(lbmatkhau);
		textServer.add(cbxmaychu);
		textServer.add(cbxcsdl);
		textServer.add(cbxmatkhau);
		return pg3;
	}
}
